package alo;

import java.util.HashMap;

/**
 * @ClassName test6
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/15 17:20
 * @Version 1.0
 */
public class test6 {
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        HashMap<Integer,Integer> map=new HashMap<>();
        for(int i = 0; i< numbers.length; i++) {
            int num = target - numbers[i];
            if(map.containsKey(num)) {     //如果hashmap中包含target - numbers[i]，则找到这两个数，返回下标
                return new int[] {map.get(num)+1,i+1};   //注意：返回的下标要从1开始
            }
            map.put(numbers[i], i);        //将numbers[]数组中的值和下标存入hashmap中
        }
        return null;
    }
}
